package com.hdoit.apollo.web.shop.apis;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.shop.model.Address;
import com.hdoit.apollo.logical.shop.service.IAddressService;
import com.hdoit.apollo.logical.viewmodel.shop.AddressSearch;
import com.tennetcn.authority.component.webapi.BaseApi;
import com.tennetcn.authority.component.webapi.ResponseModel;
import com.tennetcn.authority.component.webapi.ResponseStatus;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.enums.ModelStatus;

@Controller
@RequestMapping(value = "/api/v1/address/",method = { RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
@CrossOrigin(origins = "*", maxAge = 3600)
@Api(value="/api/v1/address")
public class AddressApi extends BaseApi{

	@Autowired
	private IAddressService addressService;
	
	private final String saveAddressApi="saveAddress";
	private final String getAddresss="getAddresss";
	
    @RequestMapping(value = saveAddressApi)
    @ApiOperation(notes = saveAddressApi, httpMethod = "POST", value = "保存客户地址信息")
    public @ResponseBody String saveAddress(Address address){
    	Address addressPan = addressService.queryModel(address.getAddressId());
    	if(addressPan==null){
    		address.setCreateDate(DateUtils.getCurrent());
            address.setCreateUserId(getLoginId());
            address.setCreateUserName(getLoginName());
            address.setDeleteMark(YesOrNoInteger.no);
            address.setModelStatus(ModelStatus.add);
    	}else{
    		address.setModifyUserId(getLoginId());
    		address.setModifyUserName(getLoginName());
    		address.setModifyDate(DateUtils.getCurrent());
    		address.setModelStatus(ModelStatus.update);
    	}
    	
    	ResponseModel rm=new ResponseModel();
    	if(!addressService.applyChange(address)){
    		rm.setStatus(ResponseStatus.dataError);
    	}
    	return rm.toJson();
    }

    @RequestMapping(value = getAddresss)
    @ApiOperation(notes = getAddresss, httpMethod = "GET", value = "获取客户地址列表")
    public @ResponseBody String getGroupInfo(AddressSearch search){
    	List<Address> addresss = addressService.queryListMP(search,null);
    	ResponseModel rm=new ResponseModel();
    	rm.put("addresss",addresss);
    	return rm.toJson();
    }

}
